

package com.kimo.shop.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kimo.shop.bean.enums.OfflineHandleEventStatus;
import com.kimo.shop.bean.model.OfflineHandleEvent;
import com.kimo.shop.bean.model.OfflineHandleEventItem;
import com.kimo.shop.bean.param.OfflineHandleEventAuditParam;
import com.kimo.shop.dao.OfflineHandleEventItemMapper;
import com.kimo.shop.dao.OfflineHandleEventMapper;
import com.kimo.shop.service.OfflineHandleEventService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Objects;

/**
 * 下线处理事件
 *
 * @author
 * @date 2019-09-20 10:44:58
 */
@Service
@AllArgsConstructor
public class OfflineHandleEventServiceImpl extends ServiceImpl<OfflineHandleEventMapper, OfflineHandleEvent> implements OfflineHandleEventService {

    private final OfflineHandleEventMapper offlineHandleEventMapper;
    private final OfflineHandleEventItemMapper offlineHandleEventItemMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void apply(Long eventId, String reapplyReason) {
        // 更新事件为申请状态
        offlineHandleEventMapper.updateToApply(eventId);
        // 添加申请记录
        OfflineHandleEventItem offlineHandleEventItem = new OfflineHandleEventItem();
        offlineHandleEventItem.setCreateTime(new Date());
        offlineHandleEventItem.setEventId(eventId);
        offlineHandleEventItem.setReapplyReason(reapplyReason);
        offlineHandleEventItemMapper.insert(offlineHandleEventItem);
    }

    @Override
    public OfflineHandleEvent getProcessingEventByHandleTypeAndHandleId(Integer eventType, Long handleId) {
        return offlineHandleEventMapper.getProcessingEventByHandleTypeAndHandleId(eventType, handleId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateToApply(Long eventId, String reapplyReason) {
        // 更新事件状态
        offlineHandleEventMapper.updateToApply(eventId);

        // 添加事件申请
        OfflineHandleEventItem offlineHandleEventItem = new OfflineHandleEventItem();
        offlineHandleEventItem.setEventId(eventId);
        offlineHandleEventItem.setReapplyReason(reapplyReason);
        offlineHandleEventItem.setCreateTime(new Date());
        offlineHandleEventItemMapper.insert(offlineHandleEventItem);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void auditOfflineEvent(OfflineHandleEventAuditParam offlineHandleEventAuditParam, Long sysUserId) {
        OfflineHandleEvent offlineHandleEvent = offlineHandleEventMapper.selectById(offlineHandleEventAuditParam.getEventId());
        // 审核通过
        if (Objects.equals(offlineHandleEventAuditParam.getStatus(), OfflineHandleEventStatus.AGREE_BY_PLATFORM.getValue())) {
            offlineHandleEvent.setStatus(OfflineHandleEventStatus.AGREE_BY_PLATFORM.getValue());
        }
        // 审核不通过
        else if (Objects.equals(offlineHandleEventAuditParam.getStatus(), OfflineHandleEventStatus.DISAGREE_BY_PLATFORM.getValue())) {
            offlineHandleEvent.setStatus(OfflineHandleEventStatus.DISAGREE_BY_PLATFORM.getValue());
        }
        offlineHandleEvent.setUpdateTime(new Date());
        offlineHandleEvent.setHandlerId(sysUserId);
        offlineHandleEventMapper.updateById(offlineHandleEvent);
    }
}
